package org.loofer.ext.component.drawable;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable.Callback;

public class DrawableDecorator extends Drawable implements Callback {
    private static final boolean DEFAULT_DITHER = true;
    private int mAlpha = 255;
    private ColorFilter mColorFilter;
    private boolean mMutated;
    private DrawableDecoratorState mState;

    public static abstract class DrawableDecoratorState extends ConstantState {
        int mChangingConfigurations;
        int mChildrenChangingConfigurations;
        boolean mDither = true;
        Drawable mDrawable;
        boolean mMutated;
        final DrawableDecorator mOwner;
        final Resources mRes;

        protected DrawableDecoratorState(DrawableDecoratorState orig, DrawableDecorator owner, Resources res) {
            this.mOwner = owner;
            this.mRes = res;
            if (orig != null) {
                this.mChangingConfigurations = orig.mChangingConfigurations;
                this.mChildrenChangingConfigurations = orig.mChildrenChangingConfigurations;
                if (orig.mDrawable != null) {
                    this.mDrawable = orig.mDrawable.getConstantState().newDrawable(res);
                    this.mDrawable.setCallback(owner);
                }
                this.mDither = orig.mDither;
                this.mMutated = orig.mMutated;
            }
        }

        public int getChangingConfigurations() {
            return this.mChangingConfigurations | this.mChildrenChangingConfigurations;
        }

        public final void setDrawable(Drawable dr) {
            if (this.mDrawable != dr) {
                if (this.mDrawable != null) {
                    this.mDrawable.setCallback(null);
                }
                if (dr != null) {
                    dr.setCallback(this.mOwner);
                    dr.mutate();
                    dr.setAlpha(this.mOwner.mAlpha);
                    dr.setVisible(this.mOwner.isVisible(), true);
                    dr.setDither(this.mDither);
                    dr.setColorFilter(this.mOwner.mColorFilter);
                    dr.setState(this.mOwner.getState());
                    dr.setLevel(this.mOwner.getLevel());
                    dr.setBounds(this.mOwner.getBounds());
                }
                this.mDrawable = dr;
                this.mChildrenChangingConfigurations = dr != null ? dr.getChangingConfigurations() : 0;
            }
        }

        public final Drawable getDrawable() {
            return this.mDrawable;
        }

        final void mutate() {
            if (this.mDrawable != null) {
                this.mDrawable.mutate();
            }
            this.mMutated = true;
        }

        public boolean canConstantState() {
            if (this.mDrawable != null) {
                return this.mDrawable.getConstantState() != null;
            } else {
                return true;
            }
        }
    }

    public void invalidateDrawable(Drawable who) {
        invalidateSelf();
    }

    public void scheduleDrawable(Drawable who, Runnable what, long when) {
        scheduleSelf(what, when);
    }

    public void unscheduleDrawable(Drawable who, Runnable what) {
        unscheduleSelf(what);
    }

    public void draw(Canvas canvas) {
        if (this.mState.mDrawable != null) {
            this.mState.mDrawable.draw(canvas);
        }
    }

    public int getChangingConfigurations() {
        return (super.getChangingConfigurations() | this.mState.mChangingConfigurations) | this.mState.mChildrenChangingConfigurations;
    }

    public boolean getPadding(Rect padding) {
        if (this.mState.mDrawable != null) {
            return this.mState.mDrawable.getPadding(padding);
        }
        return super.getPadding(padding);
    }

    public boolean setVisible(boolean visible, boolean restart) {
        boolean changed = super.setVisible(visible, restart);
        if (this.mState.mDrawable != null) {
            this.mState.mDrawable.setVisible(visible, restart);
        }
        return changed;
    }

    public void setAlpha(int alpha) {
        if (this.mAlpha != alpha) {
            this.mAlpha = alpha;
            if (this.mState.mDrawable != null) {
                this.mState.mDrawable.mutate().setAlpha(alpha);
            }
        }
    }

    public int getAlpha() {
        return this.mAlpha;
    }

    public void setColorFilter(ColorFilter cf) {
        if (this.mColorFilter != cf) {
            this.mColorFilter = cf;
            if (this.mState.mDrawable != null) {
                this.mState.mDrawable.mutate().setColorFilter(cf);
            }
        }
    }

    public void setDither(boolean dither) {
        if (this.mState.mDither != dither) {
            this.mState.mDither = dither;
            if (this.mState.mDrawable != null) {
                this.mState.mDrawable.mutate().setDither(dither);
            }
        }
    }

    public int getOpacity() {
        return this.mState.mDrawable != null ? this.mState.mDrawable.getOpacity() : -2;
    }

    public boolean isStateful() {
        return this.mState.mDrawable != null && this.mState.mDrawable.isStateful();
    }

    protected boolean onStateChange(int[] state) {
        return this.mState.mDrawable != null && this.mState.mDrawable.setState(state);
    }

    protected boolean onLevelChange(int level) {
        return this.mState.mDrawable != null && this.mState.mDrawable.setLevel(level);
    }

    protected void onBoundsChange(Rect bounds) {
        if (this.mState.mDrawable != null) {
            this.mState.mDrawable.setBounds(bounds);
        }
    }

    public int getIntrinsicWidth() {
        return this.mState.mDrawable != null ? this.mState.mDrawable.getIntrinsicWidth() : -1;
    }

    public int getIntrinsicHeight() {
        return this.mState.mDrawable != null ? this.mState.mDrawable.getIntrinsicHeight() : -1;
    }

    public int getMinimumWidth() {
        return this.mState.mDrawable != null ? this.mState.mDrawable.getMinimumWidth() : 0;
    }

    public int getMinimumHeight() {
        return this.mState.mDrawable != null ? this.mState.mDrawable.getMinimumHeight() : 0;
    }

    public ConstantState getConstantState() {
        if (!this.mState.canConstantState()) {
            return null;
        }
        this.mState.mChangingConfigurations = getChangingConfigurations();
        return this.mState;
    }

    public Drawable mutate() {
        if (!this.mMutated && super.mutate() == this) {
            this.mState.mutate();
            this.mMutated = true;
        }
        return this;
    }

    public void setDecor(Drawable decor) {
        this.mState.setDrawable(decor);
        invalidateSelf();
    }

    public Drawable getDecor() {
        return this.mState.mDrawable;
    }

    protected void setConstantState(DrawableDecoratorState state) {
        this.mState = state;
    }
}
